package com.example.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.interfacee.Ivisit;
import com.example.vo.Student;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inData();
    }

    public void inData(){
/*
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String string=getJSON();
                    System.out.println("++++++"+string);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();*/


        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://192.168.1.103:8080")
                .addConverterFactory((GsonConverterFactory.create())).build();

        Ivisit ivisit = retrofit.create(Ivisit.class);
        Call<List<Student>> Stu_info =ivisit.getlist();
        Stu_info.enqueue(new Callback<List<Student>>() {
            @Override
            public void onResponse(Call<List<Student>> call, Response<List<Student>> response) {
                Student student = (Student) response.body();
                int id = student.getId();
                String name = student.getName();
                String password = student.getPassword();
            }

            @Override
            public void onFailure(Call<List<Student>> call, Throwable t) {

            }
        });
    }

    private String getJSON() throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] arr = new byte[1024];
        int len=0;
        URL url = new URL("http://192.168.1.103:8080/examcontent/httpcontentServlet");
        HttpURLConnection conn= (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        if (conn.getResponseCode() ==200){
            InputStream is = conn.getInputStream();
            while ((len = is.read(arr)) != -1){
                out.write(arr,0,len);
            }
            is.close();
        }

        return new String(out.toByteArray());
    }


}
